package com.sduept.nwld.dataserver.rest.daily;

import com.alibaba.fastjson.JSONArray;
import com.sduept.core.dao.SuperDAO;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.model.daily.MainProtectQuit;
import com.sduept.nwld.dataserver.model.daily.OperationaRisk;
import com.sduept.nwld.dataserver.model.daily.OperationaRiskExceed;
import com.sduept.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 保护运行风险
 */
@RestController
@RequestMapping("/protectRisk")
public class ProtectRiskService {
	@Autowired
	private SuperDAO dao;

	// 保护风险管理-电网保护运行风险

	@GetMapping("/getOperationalRisks/{start}/{end}")
	public String getOperationalRisks(@PathVariable("start") String start,@PathVariable("end") String end) {
		Date s = DateUtil.getDateByString(start, DateUtil.pattern_ymd);
		Date e = DateUtil.getDateByString(end, DateUtil.pattern_ymd);
		Calendar c = Calendar.getInstance();
		c.setTime(e);
		c.add(Calendar.DAY_OF_YEAR, 1);
		c.add(Calendar.SECOND , -1);
		e = c.getTime();
		String where = " reportTime between ? and ? ";
		List<OperationaRisk> obs = dao.findByCondition(OperationaRisk.class, where, s, e);
		return net.sf.json.JSONArray.fromObject(obs).toString();
	}

	// 保护风险管理-主保护退出运行情况

	@GetMapping("/getMainProtectionQuit/{start}/{end}")
	public String getMainProtectionQuit(@PathVariable("start") String start,@PathVariable("end") String end) {
		Date s = DateUtil.getDateByString(start, DateUtil.pattern_ymd);
		Date e = DateUtil.getDateByString(end, DateUtil.pattern_ymd);
		Calendar c = Calendar.getInstance();
		c.setTime(e);
		c.add(Calendar.DAY_OF_YEAR, 1);
		c.add(Calendar.SECOND , -1);
		e = c.getTime();
		String where = " executionTime between ? and ? ";
		List<MainProtectQuit> obs = dao.findByCondition(MainProtectQuit.class, where, s, e);
		return JSONArray.toJSONString(obs);
	}

	// 超期未检

	@GetMapping("/getUnCheckedRisk/{start}/{end}")
	public String getUnCheckedRisk(@PathVariable("start") String start,@PathVariable("end") String end) {
		start = start + " 00:00:00";
		end = end + " 23:59:59";
		Integer dir = 0;
		Integer guangdong = 0;
		Integer guangxi = 0;
		Integer guizhou = 0;
		Integer hainan = 0;
		Integer yunnan = 0;
		List<OperationaRiskExceed> exceed = new ArrayList<OperationaRiskExceed>();
		OperationaRiskExceed p = new OperationaRiskExceed();
		String sql = "select sum(directTransferNum) as directTransferNum,sum(GuangDongNum) as GuangDongNum,sum(GuangXiNum) as GuangXiNum"
				+ ",sum(GuiZhouNum) as GuiZhouNum,sum(HaiNanNum) as HaiNanNum,sum(yunnanNum) as yunnanNum"
				+ " from t_daily_protectrisk_exceed where recordDate between '"
				+ start
				+ "' and '"
				+ end
				+ "'";
		List<?> obs = dao.nativeQuery(sql);
		for (Object o : obs) {
			Object[] os = (Object[]) o;
			if (os[0] != null) {
				dir = ((BigDecimal) os[0]).intValue();
				guangdong = ((BigDecimal) os[1]).intValue();
				guangxi = ((BigDecimal) os[2]).intValue();
				guizhou = ((BigDecimal) os[3]).intValue();
				hainan = ((BigDecimal) os[4]).intValue();
				yunnan = ((BigDecimal) os[5]).intValue();
			}
			p.setDirectTransferNum(dir);
			p.setGuangdongNum(guangdong);
			p.setGuangxiNum(guangxi);
			p.setGuizhouNum(guizhou);
			p.setHainanNum(hainan);
			p.setYunnanNum(yunnan);
			Integer sum = dir + guangdong + guangxi + guizhou + hainan + yunnan;
			p.setSum(sum);
			String id = p.getId();
			if (id == null || id.trim().length() == 0) {
				id = ObjectId.get().toString();
			}
			p.setId(id);
			exceed.add(p);
		}
		return JSONArray.toJSONString(exceed);
	}
}
